package com.bkgy.modularbx.rule_edit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.config.BusinessException;
import com.bkgy.modularbx.commonbx.BxCommonService;
import com.bkgy.modularbx.rule_edit.domain.RuleEdit;
import com.bkgy.modularbx.rule_edit.dto.RuleEditUpdateDto;
import com.bkgy.modularbx.rule_edit.dto.SearchCondition;
import com.bkgy.modularbx.rule_edit.mapper.RuleEditMapper;
import com.bkgy.modularbx.rule_edit.service.RuleEditService;
import com.bkgy.sys.common.entity.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Optional;

/**
 * 规则编辑表服务实现类
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class RuleEditServiceImpl extends ServiceImpl<RuleEditMapper, RuleEdit> implements RuleEditService {

    @Autowired
    private BxCommonService commonService;

    private static final String NAME = "RULE_EDIT";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Void> addRuleEdit(RuleEdit ruleEdit) {
        log.info("新增规则编辑数据: {}", ruleEdit);
        try {
            ruleEdit.setToc(new Date());
            Optional<Integer> maxIndocno = Optional.ofNullable(commonService.getMaxIndocno(NAME));
            ruleEdit.setIndocno(maxIndocno.orElse(null));
            save(ruleEdit);
            return ResultData.ResultDataSuccessSelf("添加成功", null);
        } catch (Exception e) {
            throw new BusinessException("添加失败");
        }
    }

    @Override
    public ResultData<Page<RuleEdit>> pageRuleEdit(QueryDTO<SearchCondition> queryDTO) {
        SearchCondition dto = queryDTO.getDto();
        LambdaQueryWrapper<RuleEdit> wrapper = new LambdaQueryWrapper<>();

        if (dto != null) {
            wrapper.like(StringUtils.hasText(dto.getProcessRuleDescription()), RuleEdit::getProcessRuleDescription, dto.getProcessRuleDescription())
                    .like(StringUtils.hasText(dto.getRuleName()), RuleEdit::getRuleName, dto.getRuleName())
                    .like(StringUtils.hasText(dto.getL1VariableName()), RuleEdit::getL1VariableName, dto.getL1VariableName());
        }

        Page<RuleEdit> page = new Page<>(queryDTO.getPage().getPageIndex(), queryDTO.getPage().getPageSize());
        Page<RuleEdit> result = page(page, wrapper);
        return ResultData.ResultDataSuccess(result.getRecords(), (int) result.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Void> deleteRuleEdit(Integer indocno) {
        log.info("删除规则编辑数据，ID: {}", indocno);
        RuleEdit ruleEdit = getById(indocno);
        if (ruleEdit == null) {
            throw new BusinessException("要删除的规则编辑数据不存在");
        }
        boolean b = removeById(indocno);
        return b ? ResultData.ResultDataSuccessSelf("删除成功", null) : ResultData.ResultDataFaultSelf("删除失败", null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Void> updateRuleEdit(RuleEditUpdateDto ruleEditUpdateDto) {
        log.info("更新规则编辑数据: {}", ruleEditUpdateDto);
        RuleEdit existRuleEdit = getById(ruleEditUpdateDto.getIndocno());
        if (existRuleEdit == null) {
            throw new BusinessException("要修改的规则编辑数据不存在");
        }
        RuleEdit ruleEdit = new RuleEdit();
        BeanUtils.copyProperties(ruleEditUpdateDto, ruleEdit);
        boolean b = updateById(ruleEdit);
        return b ? ResultData.ResultDataSuccessSelf("修改成功", null) : ResultData.ResultDataFaultSelf("修改失败", null);
    }

    @Override
    public ResultData<RuleEdit> findById(Integer indocno) {
        RuleEdit ruleEdit = getById(indocno);
        if (ruleEdit == null) {
            throw new BusinessException("规则编辑数据不存在");
        }
        return ResultData.ResultDataSuccess(ruleEdit, 1);
    }
}




